﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using AudioPlayer.GUI.Renderer;
using AudioPlayer.GUI.Progress;

namespace AudioPlayer.GUI.Main {
	/// <summary>
	/// Main Toolbar
	/// </summary>
	internal class MainToolBar : ToolStrip {
		#region Fields
		private ToolStripButton _buttonPlay;
		private ToolStripButton _buttonNext;
		private ToolStripButton _buttonPrev;
		private ProgressControl _progCtrl;
		private ToolStripControlHost _progHost;
		#endregion
		#region Constructor
		/// <summary>
		/// Initializes a new instance of the <see cref="MainToolBar"/> class.
		/// </summary>
		public MainToolBar() {
			this.Init();
		}
		#endregion
		#region Private Voids
		/// <summary>
		/// Inits this instance.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		private void Init() {
			if (!this.DesignMode) {
				this.Renderer = new ToolRenderer();
				this.GripStyle = ToolStripGripStyle.Hidden;
				this.CanOverflow = false;
				this.InitButtons();
			}
		}
		/// <summary>
		/// Inits the buttons.
		/// </summary>
		private void InitButtons() {
			_buttonPlay = new ToolStripButton("Play");
			_buttonNext = new ToolStripButton("Next");
			_buttonPrev = new ToolStripButton("Prev");
			_progCtrl = new ProgressControl();
			_progHost = new ToolStripControlHost(_progCtrl);
			this.Items.Add(_buttonPlay);
			this.Items.Add(new ToolStripSeparator());
			this.Items.Add(_buttonPrev);
			this.Items.Add(_buttonNext);
			this.Items.Add(new ToolStripSeparator());
			this.Items.Add(_progHost);

			//events
			_buttonPlay.Click += new EventHandler(ButtonPlay_Click);
			_buttonPrev.Click += new EventHandler(ButtonPrev_Click);
			_buttonNext.Click += new EventHandler(ButtonNext_Click);
		}

		/// <summary>
		/// Resizes the progress control.
		/// </summary>
		private void ResizeProgress() {
			int size = this.FreeSpace;

			if (size > 0) {
				_progCtrl.Width = size-1;
				_progCtrl.MaximumSize = new System.Drawing.Size(size-1, 16);
			}
		}
		#endregion
		#region Override
		/// <summary>
		/// Raises the <see cref="E:System.Windows.Forms.Control.SizeChanged"/> event.
		/// </summary>
		/// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
		protected override void OnSizeChanged(EventArgs e) {
			base.OnSizeChanged(e);
			this.ResizeProgress();
		}
		#endregion
		#region Event Handling
		/// <summary>
		/// Handles the Click event of the ButtonPlay control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void ButtonPlay_Click(object sender, EventArgs e) {
			Config.Plugins.PlayControl.Play();
		}
		/// <summary>
		/// Handles the Click event of the ButtonPrev control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void ButtonPrev_Click(object sender, EventArgs e) {
			Config.Plugins.PlayControl.Prev();
		}
		/// <summary>
		/// Handles the Click event of the ButtonNext control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void ButtonNext_Click(object sender, EventArgs e) {
			Config.Plugins.PlayControl.Next();
		}
		#endregion
		#region Property
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public ToolStripButton ButtonPlay {
			get {
				return _buttonPlay;
			}
			set {
				_buttonPlay = value;
			}
		}
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public ToolStripButton ButtonNext {
			get {
				return _buttonNext;
			}
			set {
				_buttonNext = value;
			}
		}
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public ProgressControl Progress {
			get {
				return _progCtrl;
			}
			set {
				_progCtrl = value;
			}
		}

		/// <summary>
		/// Gets the amount of free pixels
		/// </summary>
		/// <value>0 or free space.</value>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public int FreeSpace {
			get {
				if (this.Items != null && this.Items.Count > 0) {
					int retValue = 0;
					for (int i = 0; i < this.Items.Count; i++) {
						if (this.Items[i] != _progHost) {
							retValue += this.Items[i].Width;
						}
					}
					return (this.Width - retValue);
				}
				return 0;//Default
			}
		}
		#endregion
	}
}
